package com.ccom.starter.file.config;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.ccom.core.common.util.StringUtils;
import com.ccom.starter.file.properties.MinioProperty;
import com.ccom.starter.file.service.OOSService;
import com.ccom.starter.file.serviceImpl.OOSServiceAmazonImpl;
import com.ccom.starter.file.serviceImpl.OOSServiceMinioImpl;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
@EnableConfigurationProperties(value = MinioProperty.class)
public class MinioConfig {

    @Autowired
    private MinioProperty minioProperty;

    private static final String MINIO = "minio";
    private static final String AMAZON = "amazon";

    @Bean
    public AmazonS3 amazonS3() {
        AmazonS3 s3Client;
        //创建Amazon S3对象使用明确凭证
        BasicAWSCredentials credentials = new BasicAWSCredentials(minioProperty.getAccessKey(), minioProperty.getSecretKey());
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setSignerOverride(minioProperty.getSignType());//凭证验证方式
        if (minioProperty.isSecure()) {
            clientConfig.setProtocol(Protocol.HTTPS);//访问协议
        } else {
            clientConfig.setProtocol(Protocol.HTTP);//访问协议
        }
        s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withClientConfiguration(clientConfig)
                .withEndpointConfiguration(
                        new AwsClientBuilder.EndpointConfiguration(//设置要用于请求的端点配置（服务端点和签名区域）
                                minioProperty.getEndpoint() + ":" + minioProperty.getPort(),//s3服务器 172.31.186.154:8081
                                minioProperty.getRegion()))
                .withPathStyleAccessEnabled(true)//是否使用路径方式，是的话s3.xxx.sn/bucketname
                .build();
        return s3Client;
    }

    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(minioProperty.getEndpoint(), minioProperty.getPort(), minioProperty.isSecure())
                .credentials(minioProperty.getAccessKey(), minioProperty.getSecretKey())
                .build();
    }

    @Bean
    public OOSService oosService(MinioClient minioClient, AmazonS3 amazonS3) {
        log.info(">>>>>>>> minio config inited......");
        log.info(">>>> endpoint=" + minioProperty.getEndpoint() + ",Port=" + minioProperty.getPort() + ",sdkType=" + minioProperty.getSdkType());
        if (MINIO.equals(minioProperty.getSdkType())) {
            return new OOSServiceMinioImpl(minioProperty.getBucket(), minioClient, buildDataKeyValue());
        }
        if (AMAZON.equals(minioProperty.getSdkType())) {
            return new OOSServiceAmazonImpl(minioProperty.getBucket(), amazonS3, buildDataKeyValue());
        }
        return new OOSServiceMinioImpl(minioProperty.getBucket(), minioClient, buildDataKeyValue());
    }

    /**
     * 用于对接数据中心amazon s3的copyObject、putObject接口，key、value由数据中心提供，可缺省
     *
     * @return
     */
    public Map<String, String> buildDataKeyValue() {
        if (StringUtils.isBlank(minioProperty.getDataKey()) || StringUtils.isBlank(minioProperty.getDataKeyValue())) {
            return null;
        }
        Map<String, String> map = new HashMap<>();
        map.put(minioProperty.getDataKeyValue(), minioProperty.getDataKey());
        return map;
    }
}
